﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Skimpt3;
using Skimpt3.Plugins.Interface;
using ServerUpload;
using System.IO;

namespace ServerUpload
{
    class Plugin : IUploaderInterface
    {
        #region static variables
        //This is the name of the plugin as it will appear in the list of available
        //upload destinations in the program, thus it should be static.
        //EG: "Google"
        public string Name
        {
            get { return "Server Upload - Currently unstable"; }
        }

        //This is the script that the program will interact with to effect the image 
        //upload to the server, it should be static
        //EG http://www.example.com/upload.php 
        //or 
        //   http://www.example.com/ if the script is the index page 
        public string FormURL
        {
            get { return "http://my.upload.source.com/upload.php"; }
        }

        //This is the name of the file selector box that would normally appear on a web page
        //It should be static
        //EG "file"
        //In PHP, this is accessed as $_FILES["FileBoxName"]
        public string FileBoxName
        {
            get { return "fileBoxName"; }
        }
        #endregion

        #region non-static properties 
        //This stores the url pointing to the uploaded file and is used by the 
        //application to retrieve the URL
        //
        //In order for the application to function correctly, this variable must
        //be set during the upload function (using the private string)
        //
        //If the value is still null at the end of the upload method, the program will display
        // "No URL to report"
        private string strFileUrl = null;
        public string FileUrl
        {
            get { return strFileUrl; }
        }

        //This stores the url pointing to the thumbnail of the uploaded file and is used by the 
        //application to retrieve the URL
        //
        //In order for the application to function correctly, this variable must
        //be set during the upload function (using the private string)
        //
        //If the value is still null at the end of the upload method, the program will display
        // "No URL to report"
        private string strThumbUrl = null;
        public string ThumbUrl
        {
            get { return strThumbUrl; }
        }

        //This is the image object version of th image that will be uploaded
        //It is set automatically before the application executes the plugin 
        private Image imgImage = null;
        public Image Image
        {
            get
            {
                return imgImage;
            }
            set
            {
                imgImage = value;
            }
        }

        //This is the desired file name supplied by the user
        //It is automatically set before the application executes the plugin
        private string strFileName;
        public string FileName
        {
            get
            {
                return strFileName;
            }
            set
            {
                strFileName = value;
            }
        }
        #endregion

        #region upload method
        public void Upload()
        {
            string uploadURL = "";
            string uploadUser = "";
            string uploadPass = "";
            string hashName = "FALSE";

            InputBox input = new InputBox("URL to Skimpt interface");
            DialogResult res = input.ShowDialog();

            /* <URL> */
            if (res == DialogResult.OK)
            {
                uploadURL = input.Result;
            }
            /* </URL> */


            /* <USER> */
            input = new InputBox("Username");
            res = input.ShowDialog();
            if (res == DialogResult.OK)
            {
                 uploadUser = input.Result;
            }
            /* </USER> */


            /* <PASSWORD */
            input = new InputBox("Password");
            res = input.ShowDialog();

            if (res == DialogResult.OK)
            {
                uploadPass = input.Result;
            }
            /* </PASSWORD> */


            input.Dispose();


            /* <HASH> */
            Hash bHash = new Hash();
            res = bHash.ShowDialog();
            if (res == DialogResult.OK)
            {
                bool bres = bHash.Result;
                hashName = bres.ToString().ToUpper();
            }
            bHash.Dispose();
            /* </HASH> */

            PostFile post = new PostFile(uploadURL, FileName);

            try
            {
                post.AddFormElement("SentFrom", "Skimpt3"); //Essential for the script to function
                post.AddFormElement("User", uploadUser);
                post.AddFormElement("Pass", uploadPass);
                post.AddFormElement("HashName", hashName);
                post.FileBoxName = "image";
                post.ContentMIMEType = "image/jpeg";
                post.Send(imgImage);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            XmlDocument xDoc = new XmlDocument();
            try
            {
                
                xDoc.LoadXml(new System.IO.StringReader(post.Response).ReadToEnd());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            
            /**/       /**/
            /* <ERRORS> */
            XmlNodeList errorList = xDoc.GetElementsByTagName("Error");
            if (errorList.Count > 0)
            {
                MessageBox.Show(errorList[0].InnerXml);
                return;
            }
            /* </ERRORS> */
            /**/      /**/

            XmlNodeList URLS = xDoc.GetElementsByTagName("URL");
            strFileUrl = URLS[0].InnerXml;
        }

        #endregion
    }
}
